Skip to content

Returning Index instead of boolean in knuth_morris_pratt (kmp) function, making it compatible with str.find(). #9083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 28, 2023

Conversation

HetarthJain
Copy link
Contributor

@HetarthJain HetarthJain commented Sep 24, 2023

Describe your change:

Returning the index of first occurrence of a match in knuth_morris_pratt algorithm. The return type is converted from boolean to int. The function name parameters are aligned to Python naming conventions.
Fixes #9077

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword : Fixes Returning index instead of boolean when knuth_morris_pratt matches. #9077".

@rohan472000
Copy link
Contributor

Kindly tick the checkboxes while raising PR, see contributing guidelines beforehand.

@HetarthJain
Copy link
Contributor Author

Kindly tick the checkboxes while raising PR, see contributing guidelines beforehand.

I have ticked the appropriate checkboxes.

@HetarthJain
Copy link
Contributor Author

Please review the Pull request

@rohan472000
Copy link
Contributor

#9079 is also doing the same, check that first.

@rohan472000
Copy link
Contributor

Also keep your PR title descriptive, remove 9077 and add kmp in it, as viewers can understand easily.

@HetarthJain HetarthJain changed the title #9077 Returning Index instead of boolean. Returning Index instead of boolean in knuth_morris_pratt (kmp) function, making it compatible with str.find(). Sep 25, 2023
@rohan472000
Copy link
Contributor

Meanwhile, @HetarthJain you can look into that doctest which is failing in #9079.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Sep 25, 2023
@HetarthJain
Copy link
Contributor Author

Meanwhile, @HetarthJain you can look into that doctest which is failing in #9079.

I have used the doctest in the commit itself, it has passed. Please review it.

@rohan472000
Copy link
Contributor

rohan472000 commented Sep 25, 2023

Add these doctests there in kmp() function:

>>> kmp = "knuth_morris_pratt"
>>> knuth_morris_pratt(kmp, "kn") == kmp.find("kn")
True
>>> knuth_morris_pratt(kmp, "h_m") == kmp.find("h_m")
True
>>> knuth_morris_pratt(kmp, "rr") == kmp.find("rr")
True
>>> knuth_morris_pratt(kmp, "tt") == kmp.find("tt")
True
>>> knuth_morris_pratt(kmp, "not there") == kmp.find("not there")
True
>>> all(knuth_morris_pratt(kmp, s) == kmp.find(s) for s in ( "kn", "h_m", "rr", "tt", "not there" ))
True

Run: python3 -m doctest -v main.py to run doctest only (replace main.py with your filename),I checked it is passing.

@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Sep 25, 2023
@rohan472000
Copy link
Contributor

@cclauss kindly have a look at this PR, your doctest for knuth_morris_pratt is getting passed here.

@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Sep 28, 2023
@cclauss cclauss enabled auto-merge (squash) September 28, 2023 18:23
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add doctests because this PR had none.

https://docs.python.org/3/library/doctest.html

@cclauss cclauss merged commit 35dd529 into TheAlgorithms:master Sep 28, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Sep 28, 2023
@HetarthJain
Copy link
Contributor Author

Thanks @cclauss and @rohan472000 for your guidance in my first PR

sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
…on, making it compatible with str.find(). (TheAlgorithms#9083)

* Update knuth_morris_pratt.py - changed Boolean to Index

* Update knuth_morris_pratt.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update knuth_morris_pratt.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update knuth_morris_pratt.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update back_propagation_neural_network.py

* Update back_propagation_neural_network.py

* Update strings/knuth_morris_pratt.py

* Update knuth_morris_pratt.py

* Update knuth_morris_pratt.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <[email protected]>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Returning index instead of boolean when knuth_morris_pratt matches.
3 participants